package codingforgreat.lchot150;

import java.util.ArrayList;
import java.util.List;

public class Lc022 {
    public List<String> generateParenthesis(int n) {
        List<String> res = new ArrayList<>();
        if(n <=0){
            return res;
        }
        char[] path = new char[n * 2];
        process(path,0,0,n,res);
        return res;
    }
    public void process(char[] path,int index,int leftMiusRight,int left,List<String> res){
        if(index == path.length){
            res.add(String.valueOf(path));
        }
        if(left > 0){
            path[index] = '(';
            process(path,index + 1,leftMiusRight + 1,left - 1,res);
        }
        if(leftMiusRight > 0){
            path[index] = ')';
            process(path,index + 1,leftMiusRight - 1,left,res);
        }
    }
}
